﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Super_Wall_Breaker.Sources.GameEngine;

namespace Super_Wall_Breaker.Sources.TeamElements.PlayersControls
{
    /// <summary>
    /// This class defines the gamepad controls.
    /// </summary>
    public class GamePadControlsState : ControlsState
    {

        private Buttons _rightButton;
        private Buttons _leftButton;
        private Buttons _pauseButton;

        /// <summary>
        /// Initializes a new instance of the <see cref="GamePadControlsState"/> class.
        /// </summary>
        /// <param name="index">The index of the player.</param>
        public GamePadControlsState(PlayerIndex index) : base(index)
        {
            this._rightButton = GlobalConfiguration.DEFAULT_RIGHT_BUTTON;
            this._leftButton = GlobalConfiguration.DEFAULT_LEFT_BUTTON;
            this._pauseButton = GlobalConfiguration.DEFAULT_PAUSE_BUTTON;
        }

        /// <summary>
        /// This function returns the state of the gamepad.
        /// </summary>
        /// <returns>Controls.RIGHT if the player go to right, LEFT if the player go to left. PAUSE if the player press the pause button. NONE otherwise.</returns>
        public override Controls GetState()
        {
            Controls entry = Controls.NONE;
            GamePadState gps = GamePad.GetState(_playerIndex);

            if (gps.IsConnected)
            {
                if (gps.IsButtonDown(this._pauseButton) && this._previousEntry != Controls.PAUSE)
                {
                    entry = Controls.PAUSE;
                }
                else if (gps.IsButtonDown(this._rightButton) && gps.IsButtonUp(this._leftButton))
                {
                    entry = Controls.RIGHT;
                }
                else if (gps.IsButtonDown(this._leftButton) && gps.IsButtonUp(this._rightButton))
                {
                    entry = Controls.LEFT;
                }
            }

            this._previousEntry = entry;
            return entry;
        }
    }
}
